package com.egd.imapp.contoller.activity.contact;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.os.Bundle;
//import android.service.controls.actions.ModeAction;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.egd.imapp.R;
import com.egd.imapp.model.Model;
import com.egd.imapp.model.bean.UserInfo;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;

public class AddFriendActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView iv_add_back;
    private ImageView iv_add_avater;

    private TextView tv_add_search;
    private TextView tv_add_username;
    private TextView tv_add_cancel;

    private Button btn_add_add;
    private EditText et_add_input;

    private ConstraintLayout cl_add;
    private UserInfo userInfo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_friend);

        initView();

        initQuery();

        initListener();
    }

    private void initListener() {
        iv_add_back.setOnClickListener(this);
        tv_add_search.setOnClickListener(this);
        tv_add_cancel.setOnClickListener(this);
        btn_add_add.setOnClickListener(this);

    }

    private void initView() {
        iv_add_back = findViewById(R.id.iv_add_back);
        iv_add_avater = findViewById(R.id.iv_add_avater);
        tv_add_search = findViewById(R.id.tv_add_search);
        tv_add_username = findViewById(R.id.tv_add_username);
        tv_add_cancel = findViewById(R.id.tv_add_cancel);
        btn_add_add = findViewById(R.id.btn_add_add);
        et_add_input = findViewById(R.id.et_add_input);
        cl_add = findViewById(R.id.cl_add);
    }
    // 初始化搜索框获取焦点，并打开软键盘
    private void initQuery() {
        et_add_input.requestFocus();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch ( v.getId() ){
            case R.id.iv_add_back: // 返回
                onBackPressed();
                break;
            case R.id.tv_add_search:// 查询
                find();
                break;
            case R.id.tv_add_cancel: // 取消操作
                et_add_input.setText("");
                break;
            case R.id.btn_add_add: // 添加
                add();
                break;
            default:
                break;

        }
    }

    private void add() {
        Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                // 到环信服务器添加好友
                try {
                    EMClient.getInstance().contactManager().addContact(userInfo.getName(), userInfo.getName() + " 请求添加好友");

//                    EMClient.getInstance().groupManager().
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(AddFriendActivity.this, "发送添加好友邀请成功", Toast.LENGTH_SHORT).show();
                        }
                    });
                } catch (HyphenateException e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(AddFriendActivity.this, "发送添加好友邀请失败" + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });
    }

    private void find() {
        String username = et_add_input.getText().toString().trim();

        if (TextUtils.isEmpty(username)){
            Toast.makeText(AddFriendActivity.this, "输入的用户名或手机号不能为空", Toast.LENGTH_SHORT).show();;
            return;
        }

        Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                // 去服务器中查找输入的用户是否存在
                // 没有服务器，模拟查找数据
                userInfo = new UserInfo(username);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        cl_add.setVisibility(View.VISIBLE);
                        tv_add_username.setText(userInfo.getName());
                    }
                });

            }
        });
    }
}